<template>
    <div class="flip-counter">
      <div class="flip-digit" v-for="(digit, index) in digits" :key="index" :class="{ flipped: shouldFlip[index] }">
        <div class="digit-value" :style="{ '--prev-digit': prevDigits[index], '--next-digit': digit }"></div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    props: {
      value: {
        type: Number,
        required: true,
      },
    },
    data() {
      return {
        digits: [],
        prevDigits: [],
        shouldFlip: [],
      };
    },
    watch: {
      value(newVal) {
        this.updateDigits(newVal);
      },
    },
    mounted() {
      this.updateDigits(this.value);
    },
    methods: {
      updateDigits(value) {
        const newDigits = value.toString().padStart(this.digits.length, '0').split('');
        this.prevDigits = this.digits.slice();
        this.digits = newDigits;
        this.shouldFlip = newDigits.map((_, i) => this.prevDigits[i] !== newDigits[i]);
      },
    },
  };
  </script>
  
  <style scoped>
  .flip-counter {
    display: flex;
    font-size: 4rem;
    font-weight: bold;
    color: #333;
  }
  
  .flip-digit {
    position: relative;
    width: 2em;
    height: 4em;
    margin: 0 0.2em;
    overflow: hidden;
    perspective: 1000px;
  }
  
  .digit-value::before,
  .digit-value::after {
    content: attr(style);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    backface-visibility: hidden;
    transform-origin: 50% 100%;
    transition: transform 0.5s;
  }
  
  .digit-value::before {
    content: var(--prev-digit);
    transform: rotateX(0deg);
  }
  
  .digit-value::after {
    content: var(--next-digit);
    transform: rotateX(-180deg);
  }
  
  .flipped .digit-value::before {
    transform: rotateX(180deg);
  }
  
  .flipped .digit-value::after {
    transform: rotateX(0deg);
  }
  </style>